package com.yanqu.road.dao.impl.zhaoshang;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.zhaoshang.ZsRobTimes;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZsRobTimesDaoImpl extends TempDataObjectDao<ZsRobTimes> {
    public static String getZsRobTimesSql = "select * from `t_u_zs_rob_times` where `userId`=?";
    public Map<Long,ZsRobTimes> getZsRobTimes(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, ZsRobTimes> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getZsRobTimesSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsRobTimes data = parse(rs);
                dataMap.put(data.getRobUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateZsRobTimesSql = "update `t_u_zs_rob_times` set robTimes=?,lastResetTime=? where userId=? and robUserId=?";
    @Override
    protected boolean updateData(ZsRobTimes zsRobTimes) {
        zsRobTimes.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, zsRobTimes.getRobTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getLastResetTime()));
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getRobUserId()));
        if (getDb().executeNoneQuery(updateZsRobTimesSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertZsRobTimesSql = "insert into `t_u_zs_rob_times`(userId,robUserId,robTimes,lastResetTime) values (?,?,?,?)";
    @Override
    protected boolean addData(ZsRobTimes zsRobTimes) {
        zsRobTimes.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getRobUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, zsRobTimes.getRobTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, zsRobTimes.getLastResetTime()));
        if (getDb().executeNoneQuery(insertZsRobTimesSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ZsRobTimes parseData(ResultSet rs) throws SQLException {
        ZsRobTimes zsRobTimes = new ZsRobTimes();
        zsRobTimes.setUserId(rs.getLong("userId"));
        zsRobTimes.setRobUserId(rs.getLong("robUserId"));
        zsRobTimes.setRobTimes(rs.getInt("robTimes"));
        zsRobTimes.setLastResetTime(rs.getLong("lastResetTime"));

        return zsRobTimes;
    }

}
